package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgBom;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.mapper.mysql.CgBomMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.bean.JqzzMaterial;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzMaterialListMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzMaterialMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqzzPartsMaterialMapper;
import com.jy.adscm.operation.mscyb.service.JqzzMaterialListService;
import com.jy.adscm.operation.mscyb.service.JqzzMaterialService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.service.JqzzPartsMaterialListService;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.mapper.mysql.PurchasebomMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-06-02
 */
@Service
public class JqzzMaterialServiceImpl extends ServiceImpl<JqzzMaterialMapper, JqzzMaterial> implements JqzzMaterialService {
    @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Autowired
    JqzzMaterialListService jqzzMaterialListService;
    @Override
    public List<JqzzMaterial> selectByTid(String tid) {
        Wrapper<JqzzMaterial> wrapper=new EntityWrapper<>();
        wrapper.eq("tid",tid);
        List<JqzzMaterial> jqzzMaterials = super.selectList(wrapper);
        for (JqzzMaterial jqzzMaterial : jqzzMaterials) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMaterial.getName(),null)){
                CgMaterial cgMaterial = cgMaterialMapper.selectById(jqzzMaterial.getName());
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial,null)){
                    jqzzMaterial.setNameName(cgMaterial.getName());
                }
            }
        }
        return jqzzMaterials;
    }

    @Override
    public Boolean approve(String tid, Token token) throws AdscmSystemException{
        approveMaterial(tid,token);

        return true;
    }
    
    public  Boolean  approveMaterial(String tid ,Token token) throws AdscmSystemException {
        //根据这个tid 查询到这个下面的所有的装配清单
        Wrapper<JqzzMaterial> jqbzMaterialWrapper=new EntityWrapper<>() ;
        jqbzMaterialWrapper.eq("tid",tid);
        List<JqzzMaterial> jqbzMaterials = super.selectList(jqbzMaterialWrapper);
        for (JqzzMaterial jqbzMaterial : jqbzMaterials) {
            if (jqbzMaterial.getIsAssembly()==1){
                continue;
            }
            //到原料库查询对应的库存
            List<JqylMaterial> jqylMaterials = jqzzMaterialListService.queryMaterial(jqbzMaterial.getName());
            double temp=0f;

            for (JqylMaterial jqylMaterial : jqylMaterials) {
                temp=temp+jqylMaterial.getRemainingnums();
            }
            if (jqbzMaterial.getNum()>temp){
                continue;
            }
            Float num = jqbzMaterial.getNum();

            for (int i = 0; i < jqylMaterials.size(); i++) {
                JqylMaterial jqylMaterial = jqylMaterials.get(0);
                Float remainingnums = jqylMaterial.getRemainingnums();
                if (remainingnums-num>0){
                    JqzzMaterialList jqzzMaterialList =new JqzzMaterialList();
                    jqzzMaterialList.setPid(jqbzMaterial.getId());
                    jqzzMaterialList.setName(jqylMaterial.getTid().toString());
                    jqzzMaterialList.setNum(num);
                    jqzzMaterialListService.insertJqzzMaterialList(jqzzMaterialList,token);
                    break;
                }else {
                    num =num-remainingnums;
                    JqzzMaterialList jqzzMaterialList =new JqzzMaterialList();
                    jqzzMaterialList.setPid(jqbzMaterial.getId());
                    jqzzMaterialList.setName(jqylMaterial.getTid().toString());
                    jqzzMaterialList.setNum(remainingnums);
                    jqzzMaterialListService.insertJqzzMaterialList(jqzzMaterialList,token);
                }
            }
        }
        return true;
    }

}
